package com.company.city;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class CityUtils {
    public static void main(String []args){
        BufferedReader in = null;
        try {
            File f = new File("E:\\a.txt");
            FileInputStream fs;
            String result = null;
            try {
                fs = new FileInputStream(f);
                byte[] b = new byte[fs.available()];
                fs.read(b);
                fs.close();
                result = new String(b);
            } catch (Exception e) {
                e.printStackTrace();
            }


            List<CityList> newCityList = new ArrayList<>();
            List<OldList> cityList = new Gson().fromJson(result, new TypeToken<List<OldList>>() {
            }.getType());

            for (int i = 0; i < cityList.size(); i++) {

                OldList p = cityList.get(i);//省
                CityList newP = new CityList();
                newP.setAreaId(p.arid);
                newP.setAreaName(p.name);
                newP.c = p.adcode;
                newP.w = p.center_w;
                newP.h = p.center_h;



                List<CityList.CitiesBean> citiesBeans =new ArrayList<>();

                for(int j =0;j<p.city.size();j++){
                    OldListCity oldListCity =p.city.get(j);

                    CityList.CitiesBean newC=new CityList.CitiesBean();
                    newC.setAreaId(oldListCity.arid);
                    newC.setAreaName(oldListCity.name);
                    newC.c =oldListCity.adcode;
                    newC.w =oldListCity.center_w;
                    newC.h =oldListCity.center_h;
//

                    List<CityList.CitiesBean.CountiesBean> countiesBeans =new ArrayList<>();
                    for(int z =0;z<oldListCity.district.size();z++){
                        OldListRegion OldListRegion =oldListCity.district.get(z);
                        CityList.CitiesBean.CountiesBean b= new CityList.CitiesBean.CountiesBean();

                        b.setAreaId(OldListRegion.arid);
                        b.setAreaName(OldListRegion.name);
                        b.c =OldListRegion.adcode;
                        b.w =OldListRegion.center_w;
                        b.h =OldListRegion.center_h;
                        countiesBeans.add(b);

                    }
                    newC.setCounties(countiesBeans);
                    citiesBeans.add(newC);
                }
                newP.setCities(citiesBeans);

                newCityList.add(newP);


            }

            String newJson = new Gson().toJson(newCityList);

            File file = new File("E:\\", "b.txt");
            if (file.createNewFile()){
                System.out.println("文件创建成功！");
                FileWriter fileWriter = null;
                try {
                    fileWriter = new FileWriter("E:\\b.txt", true);
                    fileWriter.write(newJson);
                    fileWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException("IOException occurred. ", e);
                } finally {
                    if (fileWriter != null) {
                        try {
                            fileWriter.close();
                        } catch (IOException e) {
                            throw new RuntimeException("IOException occurred. ", e);
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
